package com.zhangchuchu.neohealth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangchuchu.neohealth.common.R;
import com.zhangchuchu.neohealth.entity.Article;
import com.zhangchuchu.neohealth.service.ArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Articles)控制层
 *
 * @author makejava
 * @since 2025-04-16 14:10:36
 */
@RestController
@RequestMapping("/article")
public class ArticlesController {
    @Resource
    private ArticleService articleService;


    /**
     * 条件查询
     */
    @GetMapping("/searchList")
    public R<Page<Article>> selectList(@RequestParam(value = "query", required = false) String query,@RequestParam int page, @RequestParam int size) {
        Page<Article> page1 = new Page<>(page,size);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();

        // 如果查询条件存在，则加入查询条件
        if (query != null && !query.trim().isEmpty()) {
            queryWrapper.like("title", query)  // 假设根据标题进行模糊查询
                    .or().like("content", query);  // 也可以根据内容进行模糊查询
        }
        System.out.println("Query: " + query);
        System.out.println("Page: " + page + ", Size: " + size);
        return R.success(articleService.page(page1,queryWrapper));
    }


    /**
     * 通过主键查询单条数据
     */
    @GetMapping("{id}")
    public R<Article> selectOne(@PathVariable Serializable id) {
        return R.success(articleService.getById(id));
    }

    /**
     * 新增数据
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody Article articles) {
        return R.success(articleService.save(articles));
    }

    /**
     * 修改数据
     */
    @PutMapping
    public R<Boolean> update(@RequestBody Article articles) {
        return R.success(articleService.updateById(articles));
    }

    /**
     * 删除数据（批量）
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("idList") List<Integer> idList) {
        return R.success(articleService.removeByIds(idList));
    }
    /**
     * 获取文章(分页)
     * @param  page 页码
     * @param  size 每页大小
     * @return IPage<Article>
     */
    @GetMapping("/list")
    public IPage<Article> getArticles(@RequestParam int page, @RequestParam int size) {
        return articleService.getArticles(page, size);
    }

    /**
     * 获取点赞数最多的四个文章
     * @return List<Article>
     */
    @GetMapping("/top4")
    public Map<String, Object> getTop4Articles() {
        List<Article> top4ArticleList = articleService.getTop4Articles();

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", top4ArticleList);
        return response;
    }
    /**
     * 点赞
     * @param userId 用户id
     * @param articleId 文章id
     * @return string
     */
    @GetMapping("/like")
    public String likeArticle(@RequestParam Integer articleId, @RequestParam Integer userId) {
        boolean success = articleService.likeArticle(userId, articleId);
        return success ? "点赞成功" : "你已经点过赞了";
    }



}
